//页面加载完毕
$(function () {

    var refundManagementItem_datagrid = $("#refundManagementItem_datagrid");
    var refundManagementItem_dialog = $("#refundManagementItem_dialog");
    var refundManagementItem_form = $("#refundManagementItem_form");

    //退款管理列表初始化
    refundManagementItem_datagrid.datagrid({
        url:"/refundManagementItem/list.do",
        fit:true,
        fitColumns:true,
        striped:true,
        pagination:true,
        rownumbers:true,
        singleSelect:true,
        toolbar:"#tb",
        columns:[[
            {field:"id", hidden:true},
            {field:"stuName", title:"学员姓名", width:80,align:'center',formatter:stuName},
            {field:"refundTime", title:"退款时间", width:80,align:'center'},
            {field:"refundAmount", title:"退款金额", width:80,align:'center'},
            {field:"clazzId", title:"班级", width:80,align:'center',formatter:clazzAA},
            {field:"expenditureUser", title:"退款人", width:80,align:'center',formatter:expenditureUser},
            {field:"entryUser", title:"录入人", width:80,align:'center',formatter:entryUser},
            {field:"paymentType", title:"支付方式", width:80,formatter:paymentType,align:'center'},
            {field:"expenditureType", title:"花费类型", width:80,align:'center',formatter:expenditureType},
            {field:"documentNo", title:"单据号", width:80,align:'center'},
            {field:"refundReason", title:"退款原因", width:80,align:'center'},
            {field:"auditState", title:"审核状态", width:80,formatter:auditState,align:'center'}
        ]]
    });
    //end

    var cmdObj = {
        //退款管理添加
        add : function (){
            //打开弹出框
            refundManagementItem_dialog.dialog("open");
            //设置标题投
            refundManagementItem_dialog.dialog("setTitle", "退款管理添加");
            //清除缓存数据
            refundManagementItem_form.form("clear");
            //取消只读
            $(".readOn").textbox('readonly',false);
            //取消隐藏按钮
            $('.dialog-button a:eq(0)').show();
        },
        //编辑操作
        edit : function () {
            //判断是否选中
            var row = refundManagementItem_datagrid.datagrid("getSelected");
            if(!row){
                $.messager.alert('温馨提示',"请选择一条查看数据", "info");
                return;
            }
            if(row.auditState == 1){
                $.messager.alert('温馨提示',"已审核,不允许操作!", "info");
                return;
            }
            refundManagementItem_dialog.dialog("open");
            //修改对话框标题
            refundManagementItem_dialog.dialog("setTitle","退款信息");

            //学员姓名
            if(row.stuName){
                row["stuName.id"] = row.stuName.id;
            }
            //班级
            if(row.clazzId){
                row["clazzId.id"] = row.clazzId.id;
            }
            //退款人
            if(row.expenditureUser){
                row["expenditureUser.id"] = row.expenditureUser.id;
            }
            //录入人
            if(row.entryUser){
                row["entryUser.id"] = row.entryUser.id;
            }
            //支付方式
            if(row.paymentType){
                row["paymentType.id"] = row.paymentType.id;
            }
            //话费类型
            if(row.expenditureType){
                row["expenditureType.id"] = row.expenditureType.id;
            }
            //数据回显
            refundManagementItem_form.form("load",row);

        },
        //查看操作
        lookView : function () {
            //判断是否选中
            var row = refundManagementItem_datagrid.datagrid("getSelected");
            if(!row){
                $.messager.alert('温馨提示',"请选择一条查看数据", "info");
                return;
            }
            refundManagementItem_dialog.dialog("open");
            //修改对话框标题
            refundManagementItem_dialog.dialog("setTitle","退款信息");

            //学员姓名
            if(row.stuName){
                row["stuName.id"] = row.stuName.id;
            }
            //班级
            if(row.clazzId){
                row["clazzId.id"] = row.clazzId.id;
            }
            //退款人
            if(row.expenditureUser){
                row["expenditureUser.id"] = row.expenditureUser.id;
            }
            //录入人
            if(row.entryUser){
                row["entryUser.id"] = row.entryUser.id;
            }
            //支付方式
            if(row.paymentType){
                row["paymentType.id"] = row.paymentType.id;
            }
            //话费类型
            if(row.expenditureType){
                row["expenditureType.id"] = row.expenditureType.id;
            }
            //数据回显
            refundManagementItem_form.form("load",row);
            //隐藏保存按钮
            $('.dialog-button a:eq(0)').hide();
            //设置只读
            $(".readOn").textbox('readonly',true);

        },
        //退款管理删除
        removeT : function () {
            //判断是否选中
            var row = refundManagementItem_datagrid.datagrid("getSelected");
            if(!row){
                $.messager.alert('温馨提示',"请选择一条删除数据", "info");
                return;
            }
            $.messager.confirm('温馨提示','您确认想要删除记录吗？',function(ret){
                if (ret){
                    //确认删除
                    $.get("/refundManagementItem/delete.do?id=" + row.id,function (data) {
                        if(data.success){
                            //删除成功
                            $.messager.confirm('温馨提示','删除成功!',function(ret){
                                //刷新数据
                                refundManagementItem_datagrid.datagrid("reload");
                            });

                        }else{
                            $.messager.alert("温馨提示",data.msg,"error");
                        }
                    })
                }
            });
        },
        //退款管理添加/更新
        save : function (){
            //表单提交url
            var url = "";
            //使用id区分保存于更新操作
            var id = $("#refundManagementItemId").val();
            if(id){
                //编辑
                url = "/refundManagementItem/update.do";
            }else{
                //添加
                url = "/refundManagementItem/save.do";
            }
            //提交表单
            refundManagementItem_form.form("submit", {
                url:url,
                success:function (data) {
                    data = $.parseJSON(data);
                    if(data.success){
                        //提示成功
                        $.messager.confirm('温馨提示',"操作成功",function(r){
                            //关闭窗口
                            refundManagementItem_dialog.dialog("close");
                            //刷新数据
                            refundManagementItem_datagrid.datagrid("reload");
                        });
                    }else{
                        $.messager.alert('温馨提示',data.msg, "error");
                        //关闭窗口
                        refundManagementItem_dialog.dialog("close");
                        //刷新数据
                        refundManagementItem_datagrid.datagrid("reload");
                    }
                }

            })
        },
        //审核
        audit : function () {
            //判断是否选中
            var row = refundManagementItem_datagrid.datagrid("getSelected");
            if(!row){
                $.messager.alert('温馨提示',"请选择一条数据", "info");
                return;
            }
            if(row.auditState == 1){
                $.messager.alert('温馨提示',"已审核", "info");
                return;
            }
            $.messager.confirm('温馨提示','是否确认审核?',function(ret){
                if (ret){
                    //确定审核
                    $.get("/refundManagementItem/audit.do?id=" + row.id,function (data) {
                        if(data.success){
                            //审核成功
                            $.messager.confirm('温馨提示','审核成功!',function(ret){
                                //刷新数据
                                refundManagementItem_datagrid.datagrid("reload");
                            });

                        }else{
                            $.messager.alert("温馨提示",data.msg,"error");
                        }
                    })
                }
            });
        },
        //高级查询  思路: 加载退款管理列表时  将多条件查询数据一同发起请求
        searchT : function () {
            var paymentTypeId = $("#paymentTypeId").val();
            var clazzId = $("#clazzId").val();
            var beginDate = $("#beginDate").val();
            var endDate = $("#endDate").val();
            //加载数据列表时 携带额外参数
            refundManagementItem_datagrid.datagrid("load",{
                paymentTypeId:paymentTypeId ,
                clazzId:clazzId ,
                beginDate:beginDate ,
                endDate:endDate
            })
        },
        //取消操作
        cancel : function (){
            refundManagementItem_dialog.dialog("close");
        },
        //刷新
        reload : function (){
            refundManagementItem_datagrid.datagrid("reload");
        }
    };

    //调用
    //给所有的按钮统一绑定事件
    $("a[data-cmd]").on("click",function () {
        var cmd = $(this).data("cmd");
        cmdObj[cmd]();
    });

    //对话框初始化
    refundManagementItem_dialog.dialog({
        width:340,
        height:440,
        buttons:"#bb",
        closable:true,
        closed:true
    });
    //end

});

//退款人格式化
function expenditureUser(value, row, index){
    return value ? value.username : "";
}
//学员名字格式化
function stuName(value, row, index){
    return value ? value.name : "";
}
//花费类型格式化
function expenditureType(value, row, index){
    return value ? value.name : "";
}
//审核状态格式化
function auditState(value, row, index){
    if(value == 0){
        return "<span style='color: red'>未审核</span>"
    }
    return "<span style='color: #00ee00;'>已审核</span>";
}
//录入员格式化
function entryUser(value, row, index){
    return value ? value.username : "";
}

//支付方式格式化
function paymentType(value, row, index){
    return value ? value.name : "";
}
//班级格式化
function clazzAA(value, row, index){
    return value ? value.name : "";
}
